<?php
class keu_coa_model extends MY_Model {
	var $model = 'keu_coa';
	
	
	function GetComboGridData()
	{
		$ls_sql	= "	SELECT * FROM (SELECT	* 
					FROM	KEU_COA
					WHERE	(ID_COA LIKE '%".post('q')."%'
							OR NAMA_COA LIKE '%".post('q')."%') ORDER BY ID_COA) z WHERE ROWNUM BETWEEN 0 AND 10";
					
		return $this->db->query($ls_sql)->result_array();
	}
	
	function GetJSON()
	{
		$keyword	= post('q');
		if($keyword=='') $keyword = post('keyword');
		
		$ls_sql	= "	SELECT	* 
					FROM	KEU_COA
					WHERE	(ID_COA LIKE '%".$keyword."%'
							OR UPPER(NAMA_COA) LIKE UPPER('%".$keyword."%'))
					ORDER BY ".(post('sort')=='' ? 'ID_COA' : post('sort'))." ".(post('order')=='' ? 'ASC' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
	
	function Add($arr_data){
		$this->db->insert('KEU_COA', $arr_data);
	}
	
	function Update($arr_data, $arr_coa){
		$this->db->update('KEU_COA', $arr_data, $arr_coa);
	}
	
	function GetSingle($as_code){
		$result = $this->db->get_where('KEU_COA', array('ID_COA' => $as_code));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_code)
	{
		$ls_sql	= "	SELECT	d.*
					FROM	KEU_COA d
					WHERE	ID_COA = '".$as_code."'";
							
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetChild($as_code){
		$result = $this->db->get_where('KEU_COA', array('ID_COA_INDUK' => $as_code));
		if($result->num_rows() == 1){
			return $result->result_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($as_code)
	{
		try {
			$this->db->delete('KEU_COA', array('ID_COA' => $as_code));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
	
	function GetComboTreeData(){
		$ls_sql	= "	SELECT	ID_COA id, NAMA_COA text, (SELECT COUNT(1) FROM KEU_COA tmp WHERE tmp.ID_COA_INDUK = d.ID_COA) numchild
					FROM	KEU_COA d
					WHERE	NVL(ID_COA_INDUK,' ') = ' '";
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			
			for($i=0,$max_i=count($arr_data); $i<$max_i; $i++){
				if(intval($arr_data[$i]['numchild']) > 0){
					$arr_data[$i]['children']	= $this->GetComboTreeSubData($arr_data[$i]['id']);
				}
			}
			
			return array(array('id' => 0, 'text' => '', 'children' => $arr_data));
		}else{
			return NULL;
		}
	}
	
	function GetComboTreeSubData($parent_id=''){
		if($parent_id=='') return NULL;
		
		$ls_sql	= "	SELECT	ID_COA id, NAMA_COA text, (SELECT COUNT(1) FROM KEU_COA tmp WHERE tmp.ID_COA_INDUK = d.ID_COA) numchild
					FROM	KEU_COA d
					WHERE	ID_COA_INDUK = '".$parent_id."'";
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			
			for($i=0,$max_i=count($arr_data); $i<$max_i; $i++){
				if(intval($arr_data[$i]['numchild']) > 0){
					$arr_data[$i]['children'] = $this->GetComboTreeSubData($arr_data[$i]['id']);
				}
			}
			
			return $arr_data;
		}else{
			return NULL;
		}
	}
	
	
	
	
	//=================================
	function GetReportData(){
		$ls_sql	= "	SELECT	coa.*, 							
							(SELECT COUNT(1) FROM KEU_COA tmp WHERE tmp.ID_COA_INDUK = coa.ID_COA) numchild,
							0 lvl
					FROM	KEU_COA coa
					WHERE	NVL(coa.ID_COA_INDUK,' ') = ' ' ";
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			
			$arr_return = array();
			
			for($i=0,$max_i=count($arr_data); $i<$max_i; $i++){
				array_push($arr_return, $arr_data[$i]);
				if(intval($arr_data[$i]['numchild']) > 0){
					$arr_return = array_merge($arr_return, $this->GetReportSubData($arr_data[$i]['id_coa'], $arr_data[$i]['lvl']));
				}
			}
			
			return $arr_return;
		}else{
			return NULL;
		}
	}
	
	function GetReportSubData($parent_id='', $ai_level=0){
		if($parent_id=='') return NULL;
		
		$ls_sql	= "	SELECT	coa.*, 							
							(SELECT COUNT(1) FROM KEU_COA tmp WHERE tmp.ID_COA_INDUK = coa.ID_COA) numchild,
							".($ai_level+1)." lvl
					FROM	KEU_COA coa
					WHERE	NVL(coa.ID_COA_INDUK,' ')= '".$parent_id."'";
							
		$result	= $this->db->query($ls_sql);					
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			
			$arr_return = array();
			
			for($i=0,$max_i=count($arr_data); $i<$max_i; $i++){
				array_push($arr_return, $arr_data[$i]);
				if(intval($arr_data[$i]['numchild']) > 0){
					$arr_return = array_merge($arr_return, $this->GetReportSubData($arr_data[$i]['id_coa'], $arr_data[$i]['lvl']));
				}
			}
			
			return $arr_return;
		}else{
			return NULL;
		}
	}
}
?>